package com.lingnan.eduservice.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingnan.eduservice.entity.po.EduSubjectPO;
import com.lingnan.eduservice.entity.vo.EduSubjectVO;
import com.lingnan.eduservice.service.EduSubjectService;
import com.lingnan.servicebase.excetioncontrooller.LingNanException;

/**
 * Excel读监听器(课程管理)
 * @author zzd
 */
public class EduSubjectExcelListener extends AnalysisEventListener<EduSubjectVO> {

    private EduSubjectService eduSubjectService = null;

    /**
     * 无参构造方法
     */
    public EduSubjectExcelListener() {

    }

    /**
     * 带参构造方法(初始化eduSubjectService)
     *
     * @param eduSubjectService
     */
    public EduSubjectExcelListener(EduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    /**
     * 逐行读操作
     * @param eduSubjectVO
     * @param analysisContext
     */
    @Override
    public void invoke(EduSubjectVO eduSubjectVO, AnalysisContext analysisContext) {
        // 判断Excel中是否存在信息
        if (eduSubjectVO == null) {
            throw new LingNanException(20002, "Excel不存在信息");
        }

        // 添加一级分类
        EduSubjectPO oneEduSubjectPO = this.judgeOneSubject(eduSubjectService, eduSubjectVO.getOneSubjectName());
        // 如果当前数据库没有该一级分类
        if (null == oneEduSubjectPO) {
            oneEduSubjectPO = new EduSubjectPO();
            // 设置分类名称
            oneEduSubjectPO.setTitle(eduSubjectVO.getOneSubjectName());
            // 设置为父类
            oneEduSubjectPO.setParentId("0");
            // 执行保存
            eduSubjectService.save(oneEduSubjectPO);
        }

        String parentId = oneEduSubjectPO.getId();

        // 添加二级分类
        EduSubjectPO twoEduSubjectPO = this.judgeTwoSubject(eduSubjectService, eduSubjectVO.getTwoSubjectName(), parentId);
        if (null == twoEduSubjectPO) {

            twoEduSubjectPO = new EduSubjectPO();

            // 设置分类名称
            twoEduSubjectPO.setTitle(eduSubjectVO.getTwoSubjectName());

            // 设置父类名称
            twoEduSubjectPO.setParentId(parentId);

            // 执行保存
            eduSubjectService.save(twoEduSubjectPO);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    /**
     * 判断一级分类是否存在
     *
     * @param eduSubjectService
     * @param title
     * @return
     */
    private EduSubjectPO judgeOneSubject(EduSubjectService eduSubjectService, String title) {
        // 创建查询条件
        QueryWrapper<EduSubjectPO> wrapper = new QueryWrapper<>();

        // 判断类别名称是否相同
        wrapper.eq("title", title);

        // 判断是否为父类别
        wrapper.eq("parent_id", "0");

        // 执行查询
        EduSubjectPO eduSubjectPO = eduSubjectService.getOne(wrapper);

        // 返回查询结果
        return eduSubjectPO;
    }

    /**
     * 判断二级分类是否存在
     *
     * @param eduSubjectService
     * @param title
     * @param parentId
     * @return
     */
    public EduSubjectPO judgeTwoSubject(EduSubjectService eduSubjectService, String title, String parentId) {
        // 创建查询条件
        QueryWrapper<EduSubjectPO> wrapper = new QueryWrapper<>();

        // 判断类别名称是否相同
        wrapper.eq("title", title);

        // 判断父类别
        wrapper.eq("parent_id", parentId);

        // 执行查询
        EduSubjectPO eduSubjectPO = eduSubjectService.getOne(wrapper);

        return eduSubjectPO;
    }
}
